/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-7         admin
 */

package com.tianci.appstore.ui.search;

import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;

/**
 * @author admin
 * @ClassName SearchKeyLayout
 * @date 2014-7-7
 */
public class SearchKeyLayout extends FrameLayout {
    private ArrayList<ArrayList<Integer>> container;
    private ArrayList<Integer> ids, pre_ids;
    private int focusId = 0;

    public int Margin_Num = 0;

    public static int lastOnClickId = -1;

    private int id, size_r = 0;
    private String tag_id = "";
    private FrameLayout.LayoutParams lp;
    public int first_x = 0, first_y = 0;
    private int x = 0, y = 0;

    public SearchKeyLayout(Context context) {
        super(context);
        container = new ArrayList<ArrayList<Integer>>();
    }

    public void setFocusItem(int id) {
        for (int i = 0; i < this.getChildCount(); i++) {
            int tagID = (Integer) this.getChildAt(i).getId();
            if (tagID == id) {
                SearchKeyItemView view = (SearchKeyItemView) this.getChildAt(i);
                if (view != null) {
                    view.setFocusable(true);
                    view.requestFocus();
                    return;
                }
                return;
            }
        }
    }

    /**
     * @param r    行数
     * @param page 页面标签名称
     */
    public void setSize(int r, int margin) {
        size_r = r;
        Margin_Num = SkyConstants.Div(margin);
        first_x = 0;
        first_y = 0;
        for (int i = 0; i < size_r; i++) {
            container.add(new ArrayList<Integer>());
        }
    }

    // query view
    public SearchKeyItemView QueryView(int index) {
        if (index < 0) {
            return null;
        }
        int r = container.size();
        int range = 0;
        for (int i = 0; i < r; i++) {
            range = container.get(i).size();

            if (index < range) {
                try {
                    return (SearchKeyItemView) this.findViewById(container.get(i).get(index));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            index -= container.get(i).size();
        }
        return null;
    }

    // add view
    public boolean addSubView(SearchKeyItemView m_v, int row, int cols) {
        LogUtils.d("zhoufeng'an", m_v.toString());

        id = row * 10 + cols;
        tag_id = String.valueOf(row * 10 + cols);
        if (row < 0 || row >= size_r || cols < 0) {
            return false;
        }

        int size = this.getChildCount();
        for (int i = 0; i < size; i++) {
            if (this.getChildAt(i).getId() == id) {
                return false;
            }
        }
        ids = container.get(row);
        if (cols > ids.size()) {
            return false;
        }

        ids.add(cols, id);

        if (cols == 0) {
            x = first_x;
        } else {
            SearchKeyItemView preView = (SearchKeyItemView) this.findViewById(ids.get(cols - 1));
            x = (int) (preView.getXPostion() + preView.getWidthSize() + Margin_Num - SkyConstants.Div(87));
        }

        if (row == 0) {
            y = first_y;
        } else {
            pre_ids = container.get(row - 1);
            int pre_x = 0;
            for (int i = 0; i < pre_ids.size(); i++) {
                SearchKeyItemView view = (SearchKeyItemView) this.findViewById(pre_ids.get(i));
                pre_x = view.getXPostion() + view.getWidthSize();
                if (x < pre_x) {
                    y = (int) (view.getYPostion() + view.getHeightSize() + Margin_Num - SkyConstants.Div(87));
                    break;
                }
            }
        }
        m_v.setId(id);
        m_v.setTag(tag_id);

        lp = new FrameLayout.LayoutParams(m_v.getWidthSize(), m_v.getHeightSize());
        lp.gravity = Gravity.TOP | Gravity.LEFT;
        lp.setMargins(x, y, 0, 0);
        m_v.setXPosition(x);
        m_v.setYPosition(y);
        this.addView(m_v, lp);
        return true;
    }

    public void setFocusId(int i) {
        focusId = i;
    }

    public int getFocusId() {
        return focusId;
    }

    public int getLastClickItemId() {
        return lastOnClickId;
    }

}
